package com.dc.seckill.config;

import com.dc.seckill.domain.MiaoshaUser;
import com.dc.seckill.redis.MiaoshaUserKey;
import com.dc.seckill.service.MiaoshaUserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @模块 参数模块
 * @作者 djs
 * @时间 2018/3/26 13:57
 * @版本 V1.0
 * @描述 对用胡信息进行预处理
 */
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {

    private Logger log = LoggerFactory.getLogger(UserArgumentResolver.class);

    @Autowired
    MiaoshaUserService userService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        Class<?> clazz = methodParameter.getParameterType();
        return clazz== MiaoshaUser.class;
    }

    /**
     * 对用户参数进行处理
     * @params [methodParameter, modelAndViewContainer, nativeWebRequest, webDataBinderFactory]
     * @returns java.lang.Object
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {

        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class);

        String cookieToken = getCookieValue(request, MiaoshaUserKey.COOKI_NAME_TOKEN);
        String paramToken = request.getParameter(MiaoshaUserKey.COOKI_NAME_TOKEN);
        if(cookieToken == null && paramToken == null){
            return null;
        }
        String token = (StringUtils.isEmpty(paramToken)?cookieToken:paramToken);
        log.info("token:"+token);
		log.info("token:"+token);
        return userService.getByToken(response,token);
    }

    /**
     * 根据名字获取cookie的值
     * @params [cookies, cookieName]
     * @returns java.lang.String
     */
    private String getCookieValue(HttpServletRequest request,String cookieName){
        log.info("获取cookie");
        Cookie[] cookies = request.getCookies();
        if(cookies == null || cookies.length <= 0 || StringUtils.isEmpty(cookieName)){
            return null;
        }
        String cookieValue = null;
        for(Cookie cookie: cookies){
            if(cookieName.equals(cookie.getName())){
                cookieValue = cookie.getValue();
            }
        }
        return cookieValue;
    }

}
